package com.onluy.conf.rabbitmq;

import com.onluy.mqcomsumer.MessageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitmqSender {

    protected Logger log =   LoggerFactory.getLogger(this.getClass());

    private final AmqpAdmin amqpAdmin;
    private final AmqpTemplate rabbitTemplate;

    @Autowired
    public RabbitmqSender(AmqpAdmin amqpAdmin, AmqpTemplate amqpTemplate) {
        this.amqpAdmin = amqpAdmin;
        this.rabbitTemplate = amqpTemplate;
    }

    public void send(MessageType messageType, Object message) {
        try {
            if(rabbitTemplate!=null)
                rabbitTemplate.convertAndSend(messageType.name(), message);
            else
                log.warn("=============rabbitTemplate is null " );
        }catch (Exception ex){
            log.error("=============MessageProducer.send error "+ex.getMessage());
            ex.printStackTrace();
        }
    }
}
